import { createSlice } from "@reduxjs/toolkit";
import { getRouteList, testTokenLogin } from "@/service/api.js";
import { message } from "antd";

export const loginSlice = createSlice({
  name: "loginStore",
  initialState: {
    routeList: [],
    loginStatus: false,
  },
  extraReducers: (builder) => {
    builder
      .addCase(getRouteList.fulfilled, (state, action) => {
        const res = action.payload;
        if (res && res?.code === 200) {
          state.routeList = res?.data;
          state.loginStatus = true;
          localStorage.setItem(
            "token",
            JSON.stringify("sdfsdfklsfksjflsfkjkls"),
          );
        } else {
          state.routeList = [];
          state.loginStatus = false;
          localStorage.removeItem("token");
        }
      })
      .addCase(getRouteList.rejected, (state, action) => {
        state.routeList = [];
        localStorage.removeItem("token");
        state.loginStatus = false;
        message.error(action.error.message);
      })
      .addCase(testTokenLogin.fulfilled, (state, action) => {
        console.log(action.payload, "双token登录信息");
      });
  },
});

export default loginSlice.reducer;
